package com.yuandengta.yomall.product.service.impl;

import com.yuandengta.yomall.product.entity.AttrEntity;
import com.yuandengta.yomall.product.service.AttrService;
import com.yuandengta.yomall.product.vo.BaseAttrs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandengta.common.utils.PageUtils;
import com.yuandengta.common.utils.Query;

import com.yuandengta.yomall.product.dao.ProductAttrValueDao;
import com.yuandengta.yomall.product.entity.ProductAttrValueEntity;
import com.yuandengta.yomall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存商品对应的规则参数
     * @param spuId 商品id
     * @param baseAttrs 规格参数集合
     */
    @Override
    public void saveProductAttrValue(Long spuId, List<BaseAttrs> baseAttrs) {
        List<ProductAttrValueEntity> collect = baseAttrs.stream().map(baseAttr -> {
            ProductAttrValueEntity entity = new ProductAttrValueEntity();
            entity.setAttrId(baseAttr.getAttrId());
            // 需要查询出属性名
            AttrEntity attrEntity = attrService.getById(baseAttr.getAttrId());
            entity.setAttrName(attrEntity.getAttrName());
            entity.setAttrValue(baseAttr.getAttrValues());
            entity.setQuickShow(baseAttr.getShowDesc());
            entity.setSpuId(spuId);

            return entity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

    /**
     * 获取SPU规格属性
     * @param spuId
     * @return
     */
    @Override
    public List<ProductAttrValueEntity> listBaseAttrForSpu(Long spuId) {
        List<ProductAttrValueEntity> attrValueEntities = this.list(
                new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        return attrValueEntities;
    }

    /**
     * 修改SPU规格属性
     * @param spuId
     * @param entities
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities) {
        // 删除spu之前对应的所有规格属性
        this.remove(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));

        // 插入spu新的规格属性
        List<ProductAttrValueEntity> attrValueEntities = entities.stream().map(productAttrValueEntity -> {
            productAttrValueEntity.setSpuId(spuId);
            return productAttrValueEntity;
        }).collect(Collectors.toList());
        this.saveBatch(attrValueEntities);
    }

}